/*
 * Alloy Analyzer 4 -- Copyright (c) 2006-2008, Felix Chang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package edu.ut.ece382c.valloy.ast;

import edu.mit.csail.sdg.alloy4.Pos;
import edu.mit.csail.sdg.alloy4.Err;
import edu.mit.csail.sdg.alloy4.JoinableList;

/**
 * Immutable; represents an expression of the form (let a=b | x).
 *
 * <p> <b>Invariant:</b>  type!=EMPTY => (var.type.unambiguos() && sub.mult==0)
 */

public final class ExprLet extends Expr {

    /** The LET variable. */
    public final ExprVar var;

    /** The body of the LET expression. */
    public final Expr sub;

    /** Caches the span() result. */
    private Pos span=null;

    //=============================================================================================================//

    /** {@inheritDoc} */
    @Override public Pos span() {
        Pos p=span;
        if (p==null) span = (p = var.span().merge(sub.span()));
        return p;
    }

    //=============================================================================================================//

    /** {@inheritDoc} */
    @Override public void toString(StringBuilder out, int indent, boolean newline) {
        //if (indent<0) {
        //    out.append("(let ").append(var.label).append("=... | ");
        //    sub.toString(out,-1);
       // 	out.append(')');
       // } else {
    	for(int i=0; i<indent && newline; i++) { out.append(' '); }
    	out.append("let ");
    	var.toString(out, indent, false);
    	out.append(" |\n");
        sub.toString(out, indent + 2, true);
    }

    //=============================================================================================================//

    /** Constructs a LET expression. */
    private ExprLet(Pos pos, ExprVar var, Expr sub, JoinableList<Err> errs) {
        super(pos, null, sub.ambiguous, sub.type, 0, var.weight+sub.weight, errs);
        this.var=var;
        this.sub=sub;
    }

    //=============================================================================================================//

    /**
     * Constructs a LET expression.
     *
     * @param pos - the position of the '=' token in the original Alloy model (or null if unknown)
     * @param var - the LET variable
     * @param sub - the body of the LET expression (which may or may not contain "var" as a free variable)
     */
    public static Expr make(Pos pos, ExprVar var, Expr sub) {
    	return new ExprLet(pos, var, sub, emptyListOfErrors);
    }

    //=============================================================================================================//

    /** {@inheritDoc} */
    public int getDepth() {
        int a=var.getDepth(), b=sub.getDepth();
        if (a>=b) return 1+a; else return 1+b;
    }

    /** {@inheritDoc} */
    @Override final<T> T accept(VisitReturn<T> visitor) throws Err { return visitor.visit(this); }
}
